package com.guojiang.guoaicodemother.service;

import com.guojiang.guoaicodemother.model.dto.appDto.AppAddRequest;
import com.guojiang.guoaicodemother.model.dto.appDto.AppAdminUpdateRequest;
import com.guojiang.guoaicodemother.model.dto.appDto.AppQueryRequest;
import com.guojiang.guoaicodemother.model.dto.appDto.AppUpdateRequest;
import com.guojiang.guoaicodemother.common.DeleteRequest;
import com.guojiang.guoaicodemother.model.entity.App;
import com.guojiang.guoaicodemother.model.entity.User;
import com.guojiang.guoaicodemother.model.vo.appVo.AppVO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.service.IService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.codec.ServerSentEvent;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * 应用 服务层。
 */
public interface AppService extends IService<App> {

    /**
     * 创建应用
     *
     * @param appAddRequest
     * @param request
     * @return
     */
    long addApp(AppAddRequest appAddRequest, HttpServletRequest request);

    /**
     * 用户更新自己的应用
     *
     * @param appUpdateRequest
     * @param request
     * @return
     */
    boolean updateMyApp(AppUpdateRequest appUpdateRequest, HttpServletRequest request);

    /**
     * 用户删除自己的应用
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    boolean deleteMyApp(DeleteRequest deleteRequest, HttpServletRequest request);

    /**
     * 获取应用视图
     *
     * @param app
     * @return
     */
    AppVO getAppVO(App app);

    /**
     * 获取查询条件
     *
     * @param appQueryRequest
     * @return
     */
    QueryWrapper getQueryWrapper(AppQueryRequest appQueryRequest);

    /**
     * 获取应用 VO 列表
     *
     * @param appList
     * @return
     */
    List<AppVO> getAppVOList(List<App> appList);

    /**
     * 根据 id 获取自己的应用详情
     *
     * @param id
     * @param request
     * @return
     */
    AppVO getMyAppVOById(long id, HttpServletRequest request);

    /**
     * 分页获取自己的应用列表
     *
     * @param appQueryRequest
     * @param request
     * @return
     */
    Page<AppVO> listMyAppVOByPage(AppQueryRequest appQueryRequest, HttpServletRequest request);

    /**
     * 分页获取精选应用列表
     *
     * @param appQueryRequest
     * @return
     */
    Page<AppVO> listGoodAppVOByPage(AppQueryRequest appQueryRequest);

    /**
     * 管理员删除任意应用
     *
     * @param deleteRequest
     * @return
     */
    boolean deleteAppForAdmin(DeleteRequest deleteRequest);

    /**
     * 管理员更新任意应用
     *
     * @param appAdminUpdateRequest
     * @return
     */
    boolean adminUpdateApp(AppAdminUpdateRequest appAdminUpdateRequest);

    /**
     * 管理员分页获取应用列表
     *
     * @param appQueryRequest
     * @return
     */
    Page<AppVO> listAppVOByPageForAdmin(AppQueryRequest appQueryRequest);

    /**
     * 管理员获取应用详情
     *
     * @param id
     * @return
     */
    AppVO getAppVOByIdForAdmin(long id);

    /**
     * 对话 AI 生成代码（最核心接口）
     *
     * @param appId
     * @param message
     * @param loginUser
     * @return
     */
    Flux<String> chatToGenCode(Long appId, String message, User loginUser);

    /**
     * 应用部署
     *
     * @param appId
     * @param loginUser
     * @return
     */
    String deployApp(Long appId, User loginUser);

    /**
     * 异步生成应用封面图
     *
     * @param appId
     * @param appDeployUrl
     */
    void generateAppScreenshotAsync(Long appId, String appDeployUrl);
}
